package com.hp.pai.comm;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleTrigger;
import org.quartz.impl.StdSchedulerFactory;


public class PaiSchedule extends HttpServlet
{

    private static final long serialVersionUID = -2638463591846713636L;

    public PaiSchedule()
    {
        super();
    }

    @Override
    public void init() throws ServletException
    {
        
        try
        {
            addMyJob();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    /**
     * Description: <br> 
     *  
     * @author zgh<br>
     * @taskId <br>
     * @throws SchedulerException <br>
     */ 
    public void addMyJob() throws Exception
    {
        SchedulerFactory sf = new StdSchedulerFactory();
        Scheduler sched = null;
        try
        {
            sched = sf.getScheduler();
        }
        catch (SchedulerException e)
        {
            
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String sConfPath = this.getClass().getResource("/").getPath();
        // 加载配置文件的参数
        FileConfiguration fileConfiguration = new FileConfiguration(sConfPath
                + "pai_profile.properties");
        // 任务开始执行时间
        String start = fileConfiguration.getValue("profile.startJobTime");
        String profileJobInterval = fileConfiguration.getValue("profile.timeInterval");
        // 间隔秒
        int timeInterval = Integer.valueOf(profileJobInterval);
        Date startTime = null;
        try
        {
            startTime = sdf.parse(start);
        }
        catch (ParseException e1)
        {
            e1.printStackTrace();
        }
        
        @SuppressWarnings("rawtypes")
        Class clazz = null;
        try
        {
            clazz = Class.forName("com.hp.pai.comm.SynIProfileProcess");
        }
        catch (ClassNotFoundException e)
        {
            
        }
        @SuppressWarnings("unchecked")
        JobDetail jobDetail = newJob(clazz).withIdentity(
                "profileProcessJob","profileProcessJob").build();
        //触发器  
        SimpleTrigger trigger = newTrigger()
                .withIdentity("profileProcessTrig",
                        "profileProcessJob")
                .startAt(startTime)
                .withSchedule(
                        simpleSchedule()
                                .withIntervalInSeconds(timeInterval)
                                .repeatForever()
                                .withMisfireHandlingInstructionNextWithExistingCount())
                .build();
        
        sched.scheduleJob(jobDetail, trigger);
        sched.start();
    }

}
